package br.ufrj.neg.evento.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.evento.bean.CustoContatoEventoBean;
import br.ufrj.neg.evento.bean.CustoGeralBean;
import br.ufrj.neg.evento.model.CustosDAO;

public class SalvarCustosEventoContatoAction extends VelocityViewServlet {

		/**
	 * 
	 */
	private static final long serialVersionUID = -1975552875019621160L;

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/json; charset:utf-8");
		response.setCharacterEncoding("utf-8");

		saveAll(request,response);

	}


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}
	
	private void saveAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		//PrintWriter out = response.getWriter(); 
		
		String participantes = request.getParameter("rowsParticipantes");
		String gerais = request.getParameter("rowsGerais");
		Integer idEvento = Integer.parseInt(request.getParameter("idEvento"));

		
		JSONArray itemsParticipante = JSONArray.fromObject(participantes);

		JSONArray itemsGerais = JSONArray.fromObject(gerais);
		
		//Outra
		
		ArrayList<CustoContatoEventoBean> custosParticipantes = (ArrayList<CustoContatoEventoBean>) JSONArray.toCollection(itemsParticipante, CustoContatoEventoBean.class);
		ArrayList<CustoGeralBean> custosGerais = (ArrayList<CustoGeralBean>) JSONArray.toCollection(itemsGerais, CustoGeralBean.class);
				
		
		if(custosParticipantes.size()!=0){
			CustosDAO.saveParticipantes(custosParticipantes);
		}
		if(custosGerais.size()!=0){
			CustosDAO.saveCustosGerais(custosGerais,idEvento);
		}


	}
	
}
